package com.ruoyi.basedocument.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.basedocument.domain.BomCategory;
import com.ruoyi.basedocument.domain.BomMaterialhalf;
import com.ruoyi.basedocument.service.IBomCategoryService;
import com.ruoyi.basedocument.service.IBomMaterialhalfService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.util.importRunnable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basedocument.domain.BomAsscategory;
import com.ruoyi.basedocument.service.IBomAsscategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * BOM类别关联Controller
 * 
 * @author ruoyi
 * @date 2024-09-23
 */
@RestController
@RequestMapping("/bomcostestimation/basedocument/asscategory")
public class BomAsscategoryController extends BaseController
{
    @Autowired
    private IBomAsscategoryService bomAsscategoryService;
    @Autowired
    private IBomCategoryService bomCategoryService;
    @Autowired
    private IBomMaterialhalfService bomMaterialhalfService;
    @Autowired
    private importRunnable importRunnable;

    /**
     * 查询BOM类别关联列表
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:asscategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(BomAsscategory bomAsscategory)
    {
        startPage();
        List<BomAsscategory> list = bomAsscategoryService.selectBomAsscategoryList(bomAsscategory);
        return getDataTable(list);
    }

    /**
     * 导出BOM类别关联列表
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:asscategory:export')")
    @Log(title = "BOM类别关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BomAsscategory bomAsscategory)
    {
        List<BomAsscategory> list = bomAsscategoryService.selectBomAsscategoryList(bomAsscategory);
        ExcelUtil<BomAsscategory> util = new ExcelUtil<BomAsscategory>(BomAsscategory.class);
        util.exportExcel(response, list, "BOM类别关联数据");
    }

    /**
     * 导出BOM类别关联列表
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:asscategory:export')")
    @Log(title = "BOM类别关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export2")
    public void export2(HttpServletResponse response, BomAsscategory bomAsscategory)
    {
        List<BomAsscategory> list = bomAsscategoryService.selectBomAsscategoryList3(bomAsscategory);
        ExcelUtil<BomAsscategory> util = new ExcelUtil<BomAsscategory>(BomAsscategory.class);
        util.exportExcel(response, list, "BOM类别关联数据");
    }

    /**
     * 获取BOM类别关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:asscategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(bomAsscategoryService.selectBomAsscategoryById(id));
    }

    /**
     * 新增BOM类别关联
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:asscategory:add')")
    @Log(title = "BOM类别关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BomAsscategory bomAsscategory)
    {
        return toAjax(bomAsscategoryService.insertBomAsscategory(bomAsscategory));
    }

    /**
     * 修改BOM类别关联
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:asscategory:edit')")
    @Log(title = "BOM类别关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BomAsscategory bomAsscategory)
    {
        return toAjax(bomAsscategoryService.updateBomAsscategory(bomAsscategory));
    }

    /**
     * 删除BOM类别关联
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:asscategory:remove')")
    @Log(title = "BOM类别关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(bomAsscategoryService.deleteBomAsscategoryByIds(ids));
    }

    /**
     * 导入数据
     */
    @Log(title = "类别", businessType = BusinessType.INSERT)
    @PostMapping(value = "/importData")
    public AjaxResult importData(@RequestBody List<BomAsscategory> list) {
        importRunnable.removeParam();
        importRunnable.setBomAsscategoryList(list);
        Thread thread = new Thread(importRunnable);
        thread.start();
        return toAjax(1);
//        for (int i = 0; i < list.size(); i++) {
//            BomAsscategory bomAsscategory = list.get(i);
//            if(StringUtils.isNotBlank(bomAsscategory.getMaterialbomnumber())&& StringUtils.isNotBlank(bomAsscategory.getCategorynumber())){
//                BomCategory bomCategory = new BomCategory();
//                bomCategory.setNumber(bomAsscategory.getCategorynumber());
//                List<BomCategory> bomCategories = bomCategoryService.selectBomCategoryList2(bomCategory);
//                if(bomCategories==null||bomCategories.size()==0){
//                    continue;
//                }
//                BomMaterialhalf bomMaterialhalf = new BomMaterialhalf();
//                bomMaterialhalf.setCfmaterialnumber(bomAsscategory.getMaterialbomnumber());
//                List<BomMaterialhalf> bomMaterialhalves = bomMaterialhalfService.selectBomMaterialhalfList2(bomMaterialhalf);
//                if(bomMaterialhalves==null||bomMaterialhalves.size()==0){
//                    continue;
//                }
//                BomAsscategory bomAsscategory1 = new BomAsscategory();
//                bomAsscategory1.setMaterialbomnumber(bomAsscategory.getMaterialbomnumber());
//                List<BomAsscategory> bomAsscategories = bomAsscategoryService.selectBomAsscategoryList2(bomAsscategory1);
//                if(bomAsscategories!=null&&bomAsscategories.size()!=0){
//                    for (int i1 = 0; i1 < bomAsscategories.size(); i1++) {
//                        bomAsscategoryService.deleteBomAsscategoryById(bomAsscategories.get(i1).getId());
//                    }
//                }
//                bomAsscategoryService.insertBomAsscategory(bomAsscategory);
//            }
//        }
    }
}
